/* 1964 */

clear
use "$sourcedatadir/uk/ICPSR_07250/DS0001/07250_0001_data.dta"
do "$scriptsdir/uk_prepare1964.do"
save "$gendatadir/uk_1964.dta", replace


/* 1966 (Labour incumbent) */

clear
use "$sourcedatadir/uk/ICPSR_07250/DS0001/07250_0001_data.dta"
do "$scriptsdir/uk_prepare1966.do"
save "$gendatadir/uk_1966.dta", replace


/* 1970 (Labour Incumbent) */

clear
use "$sourcedatadir/uk/STATA Files (BES 1970-1997)/BES 1969-1970, 1974/UKDA-422-stata8/stata8/69_70_74.dta"
do "$scriptsdir/uk_prepare1970.do"
save "$gendatadir/uk_1970.dta", replace


/* 1974, Feb. (Tory incumbent) */

clear
use "$sourcedatadir/uk/STATA Files (BES 1970-1997)/BES 1969-1970, 1974/UKDA-359-stata8/stata8/feb74.dta"
do "$scriptsdir/uk_prepare1974Feb.do"
save "$gendatadir/uk_19741.dta", replace


/* 1979 (Labour incumbent) */

clear
use "$sourcedatadir/uk/STATA Files (BES 1970-1997)/BES 1979/UKDA-1533-stata8/stata8/may79.dta"
do "$scriptsdir/uk_prepare1979.do"
save "$gendatadir/uk_1979.dta", replace


/* 1983 (Tory incumbent) */

clear
use "$sourcedatadir/uk/STATA Files (BES 1970-1997)/BES 1983/UKDA-2005-stata6/stata6/bes83.dta"
do "$scriptsdir/uk_prepare1983.do"
save "$gendatadir/uk_1983.dta", replace


/* 1987 (Tory incumbent) */

clear
use "$sourcedatadir/uk/STATA Files (BES 1970-1997)/BES 1987/UKDA-2568-stata6/stata6/bes87e.dta"
do "$scriptsdir/uk_prepare1987.do"
save "$gendatadir/uk_1987.dta", replace


/* 1992 (Tory incumbent) */

clear
use "$sourcedatadir/uk/STATA Files (BES 1970-1997)/BES 1992/UKDA-2981-stata6/stata6/bes92e.dta"
do "$scriptsdir/uk_prepare1992.do"
save "$gendatadir/uk_1992.dta", replace



/* 1997 (Tory incumbent) */

clear
use "$sourcedatadir/uk/STATA Files (BES 1970-1997)/BES 1997/UKDA-3887-stata8/stata8/besx97e.dta"
do "$scriptsdir/uk_prepare1997.do"
save "$gendatadir/uk_1997.dta", replace


/* 2001 (Labour incumbent) */

clear
use "$sourcedatadir/uk/UKDA-4619-spss/spss/xsectionagg.dta"
do "$scriptsdir/uk_prepare2001.do"
save "$gendatadir/uk_2001.dta", replace


/* 2005 (Labour incumbent) */

clear
use "$sourcedatadir/uk/UKDA-5494-spss/spss/spss12/nc060725.dta"
do "$scriptsdir/uk_prepare2005.do"
save "$gendatadir/uk_2005.dta", replace


/* 2010 (Labour incumbent) */

clear
use "$sourcedatadir/uk/prepostmailoct2411.dta"
do "$scriptsdir/uk_prepare2010.do"
save "$gendatadir/uk_2010.dta", replace


/* Merging */

clear
use "$gendatadir/uk_1964.dta"
keep $keepers1964
append using "$gendatadir/uk_1966.dta",  keep($keepers1966) nol
append using "$gendatadir/uk_1970.dta",  keep($keepers1970) nol
append using "$gendatadir/uk_19741.dta", keep($keepers19741) nol
append using "$gendatadir/uk_1979.dta",  keep($keepers1979) nol
append using "$gendatadir/uk_1983.dta",  keep($keepers1983) nol
append using "$gendatadir/uk_1987.dta",  keep($keepers1987) nol
append using "$gendatadir/uk_1992.dta",  keep($keepers1992) nol
append using "$gendatadir/uk_1997.dta",  keep($keepers1997) nol
append using "$gendatadir/uk_2001.dta",  keep($keepers2001) nol
append using "$gendatadir/uk_2005.dta",  keep($keepers2005) nol
append using "$gendatadir/uk_2010.dta",  keep($keepers2010) nol

for any id year pid_ce income3 vote_ce turnout wt \ any "Respondent ID (within study)" "Year" "Incumbent PID" "Income (approx. terciles)" "Incumbent vote" "Turnout" "Weight": la var X "Y"

gen class_resp_gh2grade_mapped  = class_resp_goldheath 
recode class_resp_gh2grade_mapped  1=1 2=2 3=4 4=4 5=3 6=3 7=3 8=3 9=5 10=6 11=6
gen class_spou_gh2grade_mapped  = class_spou_goldheath 
recode class_spou_gh2grade_mapped  1=1 2=2 3=4 4=4 5=3 6=3 7=3 8=3 9=5 10=6 11=6 -3=.
gen class_resp_occ2grade_mapped = class_resp_occ 
recode class_resp_occ2grade_mapped 1=1 2=2 3=4 4=4 5=3 6=3 7=5 8=6 9=. 10=.
gen class_spou_occ2grade_mapped = class_spou_occ 
recode class_spou_occ2grade_mapped 1=1 2=2 3=4 4=4 5=3 6=3 7=5 8=6 9=. 10=.

gen marriedman   = 1 if married == 1 & male == 1
gen marriedwoman = 1 if married == 1 & male == 0

gen     class_hh_combi = class_head_grade
replace class_hh_combi = class_resp_grade     if male == 1 & class_hh_combi == .
replace class_hh_combi = class_resp_grade     if male == 0 & marriedwoman == 0 & class_hh_combi == .
replace class_hh_combi = class_spou_grade     if marriedwoman == 1 & class_hh_combi == .
replace class_hh_combi = class_resp_gh2grade  if male == 1 & class_hh_combi == .
replace class_hh_combi = class_resp_gh2grade  if male == 0 & marriedwoman == 0 & class_hh_combi == .
replace class_hh_combi = class_spou_gh2grade  if marriedwoman == 1 & class_hh_combi == .
replace class_hh_combi = class_resp_occ2grade if male == 1 & class_hh_combi == .
replace class_hh_combi = class_resp_occ2grade if male == 0 & marriedwoman == 0 & class_hh_combi == .
replace class_hh_combi = class_spou_occ2grade if marriedwoman == 1 & class_hh_combi == .

gen class3 = class_hh_combi
recode class3 1=2 2=2 3=1 4=1 5=0 6=0

gen     incumbent_ce_con = 0
replace incumbent_ce_con = 1 if year == 1964 | year == 19741 | year == 1983 | year == 1987 | year == 1992 | year == 1997

gen     class_of_ce = 1 if class_hh_combi <= 3 & incumbent_ce_con == 1 & class_hh_combi != .
replace class_of_ce = 0 if class_hh_combi >  3 & incumbent_ce_con == 1 & class_hh_combi != .
replace class_of_ce = 1 if class_hh_combi >  3 & incumbent_ce_con == 0 & class_hh_combi != .
replace class_of_ce = 0 if class_hh_combi <= 3 & incumbent_ce_con == 0 & class_hh_combi != .

gen     class_of_ce2 = 1 if class_hh_combi < 3 & incumbent_ce_con == 1 & class_hh_combi != .
replace class_of_ce2 = 0 if class_hh_combi > 4 & incumbent_ce_con == 1 & class_hh_combi != .
replace class_of_ce2 = 1 if class_hh_combi > 4 & incumbent_ce_con == 0 & class_hh_combi != .
replace class_of_ce2 = 0 if class_hh_combi < 3 & incumbent_ce_con == 0 & class_hh_combi != .

gen     unioninfluenced = unionmem_resp
replace unioninfluenced = unionmempast_resp if unioninfluenced == . | unioninfluenced == 0 & unionmempast_resp != .
replace unioninfluenced = unionmem_hous     if unioninfluenced == . | unioninfluenced == 0 & unionmem_hous != .
replace unioninfluenced = unionmem_spou     if unioninfluenced == . | unioninfluenced == 0 & unionmem_spou != .
replace unioninfluenced = unionmem_husb     if unioninfluenced == . | unioninfluenced == 0 & unionmem_husb != .
replace unioninfluenced = unionmem_wife     if unioninfluenced == . | unioninfluenced == 0 & unionmem_wife != .
replace unioninfluenced = unionmempast_spou if unioninfluenced == . | unioninfluenced == 0 & unionmempast_spou != .

gen     unioncouple = unionmem_resp
*replace unioncouple = unionmempast_resp if unioncouple == . | unioncouple == 0
replace unioncouple = unionmem_hous     if unioncouple == . | unioncouple == 0 & unionmem_hous != .
replace unioncouple = unionmem_spou     if unioncouple == . | unioncouple == 0 & unionmem_spou != .
replace unioncouple = unionmem_husb     if unioncouple == . | unioncouple == 0 & unionmem_husb != .
replace unioncouple = unionmem_wife     if unioncouple == . | unioncouple == 0 & unionmem_wife != .
*replace unioncouple = unionmempast_spou if unioncouple == . | unioncouple == 0

recode econ_ego_retro -1=.

save "$gendatadir/uk_cdf.dta", replace
